<template>
  <!-- 现在的首页是home/index.vue      -->
  <div class="app-container">
    <el-row class="search-container">
      <el-col :span="6">
        <el-input
          v-model="positionName"
          placeholder="岗位名称"
          @keyup.enter.native="fetchData"
        ></el-input>
      </el-col>
      <el-button type="primary" @click="fetchData()" class="search-button"
        >查询</el-button
      >
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      class="elTable"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="岗位名称" width="300">
        <template slot-scope="scope">
          <router-link
            class="positionName"
            :to="{
              name: 'position-detail',
              path: '/position-detail',
              query: { id: scope.row.ylPositionId, name: scope.row.jobName }
            }"
            target="_blank"
            rel="opener"
          >
            {{ scope.row.positionName }}</router-link
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="ID"
        width="300"
        prop="ylPositionId"
      >
        <template slot-scope="scope">
          {{ scope.row.ylPositionId }}
        </template>
      </el-table-column>
      <el-table-column label="职位数量" width="300" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.positionNum }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="tablePage.pageNum"
        :page-size="tablePage.pageSize"
        :page-sizes="pageSizes"
        :total="tablePage.total"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script>
import { getList } from '@/api/position';

export default {
  name: 'Position',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      tablePage: {
        pageNum: 1, // 第几页
        pageSize: 20, // 每页多少条
        total: 0 // 总记录数
      },
      positionName: '',
      pageSizes: [10, 20, 30]
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      let params = {
        page: this.tablePage.pageNum,
        perPage: this.tablePage.pageSize
      };
      if (this.positionName != '') {
        params.keyWord = this.positionName;
      }
      getList(params).then((response) => {
        const count = response.data.count - 0;
        this.tablePage.total = count;
        this.list = response.data.rows;
        this.listLoading = false;
      });
    },
    handlePageChange(currentPage) {
      this.tablePage.pageNum = currentPage;
      this.fetchData();
      // 在此刷新数据
    },
    handleSizeChange(pageSize) {
      this.tablePage.pageSize = pageSize;
      this.fetchData();
      // 在此刷新数据
    }
  }
};
</script>

<style scoped>
.search-container {
  margin-bottom: 24px;
}
.search-button {
  margin-left: 24px;
}
.pagination {
  margin-top: 24px;
}
::v-deep .el-table th > .cell {
  color: rgba(0, 0, 0, 0.85);
}
::v-deep .el-table th {
  background-color: #fafafa;
}
.elTable {
  width: 901px;
}
.positionName {
  color: #409eff;
}
</style>
